package com.haoyun.mirage.user.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haoyun.common.constant.CommonConstant;
import com.haoyun.common.controller.BaseController;
import com.haoyun.common.core.Result;
import com.haoyun.mirage.user.common.utils.TreeUtil;
import com.haoyun.mirage.user.model.dto.MenuDTO;
import com.haoyun.mirage.user.model.entity.SysMenu;
import com.haoyun.mirage.user.model.vo.MenuTree;
import com.haoyun.mirage.user.model.vo.MenuVO;
import com.haoyun.mirage.user.service.ISysMenuService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author twg
 * @since 2018-05-30
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService menuService;

    /**
     * 通过角色名获取菜单
     *
     * @param roleCode
     * @return
     */
    @GetMapping(value = "findMenuByRole/{roleCode}")
    public List<MenuDTO> findMenuByRole(@PathVariable("roleCode") String roleCode) {
        return menuService.findMenuByRoleCode(roleCode);
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping("tree")
    public List<MenuTree> getTree() {
        SysMenu condition = new SysMenu();
        condition.setIsDeleted(CommonConstant.STATUS_NORMAL);
        return getMenuTree(menuService.selectList(new EntityWrapper<>(condition)), 0L);
    }


    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public MenuVO menu(@PathVariable Long id) {
        MenuVO menu = new MenuVO();
        SysMenu sysMenu = menuService.selectById(id);
        BeanUtil.copyProperties(sysMenu, menu);
        return menu;
    }

    /**
     * 新增菜单
     *
     * @param menu 菜单信息
     * @return success/false
     */
    @PostMapping("add")
    public Result<String> menu(@RequestBody @Valid MenuVO menu, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtil.copyProperties(menu, sysMenu);
        menuService.insertMenu(sysMenu);
        return Result.buildSuccessResult("添加成功");
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> menuDel(@PathVariable Long id) {
        return Result.buildSuccessResult(menuService.deleteMenu(id, findRoles().toArray(new String[]{})));
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @param bindingResult
     * @return
     */
    @PutMapping("update")
    public Result<String> menuUpdate(@RequestBody @Valid MenuVO menu, BindingResult bindingResult) {
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            return Result.buildErrorResult(fieldError.getDefaultMessage());
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtil.copyProperties(menu, sysMenu);
        menuService.updateMenuById(sysMenu);
        return Result.buildSuccessResult("");
    }


    private List<MenuTree> getMenuTree(List<SysMenu> menus, Long root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (SysMenu menu : menus) {
            node = new MenuTree();
            node.setId(menu.getId());
            node.setParentId(menu.getParentId());
            node.setName(menu.getMenuName());
            node.setUrl(menu.getMenuUrl());
            node.setPath(menu.getMenuPath());
            node.setCode(menu.getPermission());
            node.setLabel(menu.getMenuName());
            node.setIcon(menu.getMenuIcon());
            trees.add(node);
        }
        return TreeUtil.build(trees, root);
    }

}

